package com.shanshan.wheel.rpc.common.remoting.dto;

import com.shanshan.wheel.rpc.common.remoting.constant.RpcResponseCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Description
 * @ClassName RpcResponse
 * @Author 山山大魔王
 * @Date 2022/9/4 16:19
 * @Version 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RpcResponse<T> implements Serializable {
    // 响应id
    private String requestId;
    // 响应码
    private Integer code;
    // 响应信息
    private String message;
    // 返回数据
    private T data;

    /**
     * 成功响应
     * @param data
     * @param requestId
     * @param <T>
     * @return
     */
    public static <T> RpcResponse<T> success(T data, String requestId){
        RpcResponse<T> response = new RpcResponse<>();
        response.setCode(RpcResponseCode.SUCCESS.getCode());
        response.setMessage(RpcResponseCode.SUCCESS.getMessage());
        response.setRequestId(requestId);
        if(null != data){
            response.setData(data);
        }

        return response;
    }

    /**
     * 失败响应
     * @param rpcResponseCode
     * @param <T>
     * @return
     */
    public static <T> RpcResponse<T> fail(RpcResponseCode rpcResponseCode){
        RpcResponse<T> response = new RpcResponse<>();
        response.setCode(rpcResponseCode.getCode());
        response.setMessage(rpcResponseCode.getMessage());
        return response;
    }


}
